package com.example.jixeishi1.controller;


import com.example.jixeishi1.entity.JsonResult1;
import com.example.jixeishi1.entity.User;
import com.example.jixeishi1.service.HRSShangPinService;
import com.example.jixeishi1.service.LoginService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/*
登录控制器
 */
@Controller
public class LoginController {
//    秒杀
//    随机送礼品
@Autowired
HttpSession session;
@Autowired
    LoginService service;
    @Autowired
    HRSShangPinService hrsShangPinService;
    @Autowired
    JavaMailSenderImpl mailSender;
@RequestMapping("/a")
@ResponseBody
public JsonResult1 verifyuser(@RequestBody User user){
    session.setAttribute("userst",user);
    System.out.println("名称");
    String yhm = null;
    String mm = null;
    List<User> listd = null;
    int qx = 0;
    User user1 = new User();
    user1.setUsername(user.getUsername());
    if (service.getuser(user) != null) {
        listd = service.getuser(user);
        for (User user2 : listd) {
            yhm = user2.getUsername();
            mm = user2.getPassword();
            qx = user2.getRole();

        }
    }
    String cg="成功";
    String sb="失败";
    if(listd.size()>0){
        JsonResult1 json=new JsonResult1(200,"",cg);
        return  json;
    }else {
        JsonResult1 json=new JsonResult1(200,"",sb);
        return  json;
    }

}

@RequestMapping("/zcaen")
@ResponseBody
public JsonResult1 a(@RequestBody User user) {
//    System.out.println(jsu);
    Calendar c = Calendar.getInstance();
    String time = new SimpleDateFormat("yyyy-MM-ddHHmmss").format(c.getTime()).toString();
    StringBuffer sjs = new StringBuffer(time.substring(14, 16));
    Long sys = System.currentTimeMillis();
    sjs.append(sys.toString().substring(11, 13));
    System.out.println();
    Double tm = Math.random() * 10000 + 2;
    sjs.append(tm.toString().substring(tm.toString().length() - 4, tm.toString().length()));
    System.out.println();
    System.out.println(sjs);
    String st = user.getUsername() + sjs;
    System.out.println(st);
    session.setAttribute("zhanghao",st);
//    map.put("zh", st);
//    ServletContext app = se.getServletContext();
//    app.setAttribute("zh", st);
    String cg="成功";
    String sb="失败";

        JsonResult1 json=new JsonResult1(200,"",cg);
     return json;
}


    @RequestMapping("/zhanhao12")
    public String zhanhao12(ModelMap map)  {
        System.out.println("黄日升");
  Object s=  session.getAttribute("zhanghao");
        map.put("zh",s);
        System.out.println(s);
return  "ZOUche";
    }





    //登录
    @GetMapping("/login")
    public ModelAndView login( ModelMap map) throws IOException {
        User user= (User) session.getAttribute("userst");
        System.out.println(user);
        System.out.println("账号" + user.getUsername());
        System.out.println("密码" + user.getPassword());
        String yhm = null;
        String mm = null;
        List<User> listd = null;
        int qx = 0;
        User user1 = new User();
        user1.setUsername(user.getUsername());
        if (service.getuser(user) != null) {
            listd = service.getuser(user);
            for (User user2 : listd) {
                yhm = user2.getUsername();
                mm = user2.getPassword();
                qx = user2.getRole();

            }
        }

        if (yhm.equals(user.getUsername()) && mm.equals(user.getPassword())) {
            System.out.println("权限" + qx);
            if (qx == 1) {
                //普通用户
                System.out.println("普通用户");
                System.out.println("成功");
                List list = hrsShangPinService.getList();
                System.out.println("用户名" + listd + "密码" + "<br/>" + list);
//                System.out.println(list);
                map.put("int1", list);
                map.put("ints1", listd);

                return new ModelAndView("hrszhuyei");
                //成功跳主业
            } else {
                //管理员
                System.out.println("管理员");
                return new ModelAndView("hrszhuyei");
            }
        } else {
            System.out.println("失败");
            //登录
            return new ModelAndView("enroll2");
        }

    }





//    判断注册的时候用户名是否存在
    @ResponseBody
    @RequestMapping("/verifyuser")
    public String verifyuser(@Param("name") String name){
        System.out.println("名称"+name);
        User user=new User();
        user.setUsername(name);
        List list=service.getuser(user);
//        f 是有
        if(list.isEmpty()==false){
            System.out.println("1");
            return "1";
        }else {
            System.out.println("0");
            return "0";
        }
    }

    @RequestMapping("/zcaen1")
    @ResponseBody
    public JsonResult1 a1(@RequestBody User user) throws MessagingException {
    System.out.println(user);
        int count=1;//默认发送一次

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        while(count--!=0){
            String codeNum = "";
            int [] code = new int[3];
            Random random = new Random();
            //自动生成验证码
            for (int i = 0; i < 6; i++) {
                int num = random.nextInt(10) + 48;
                int uppercase = random.nextInt(26) + 65;
                int lowercase = random.nextInt(26) + 97;
                code[0] = num;
                code[1] = uppercase;
                code[2] = lowercase;
                codeNum+=(char)code[random.nextInt(3)];
            }
            System.out.println(codeNum);

            //标题
            helper.setSubject("您的验证码为："+codeNum);
            //内容
            helper.setText("。您的验证码为："+"<h2>"+codeNum+"</h2>"+"千万不能告诉别人哦！",true);
            //邮件接收者
            helper.setTo(user.getUsername()+"@qq.com");
            //邮件发送者，必须和配置文件里的一样，不然授权码匹配不上
            helper.setFrom("2125904217@qq.com");
            mailSender.send(mimeMessage);
            System.out.println("邮件发送成功！"+(count+1));
            session.setAttribute("youxianhaoma",codeNum);
        }
        String cg="发送成功";
//        String sb="失败";

        JsonResult1 json=new JsonResult1(200,"",cg);
        return json;
    }

    //注册
    @RequestMapping("/enroll")
    public String enroll( String username,String password,String identify){
        System.out.println(username);
        System.out.println(password);
        System.out.println(identify);
        Object youxianhaoma= session.getAttribute("youxianhaoma");
        System.out.println(youxianhaoma);
        if(youxianhaoma.equals(identify)){
            User user=new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setRole(1);
            user.setCreatetime(new Date());
            int n=service.intouser(user);
            System.out.println(n);

            List<User> ci=hrsShangPinService.getListci();

            System.out.println(ci.get(0).getId());

            System.out.println("------"+ci);
//        ci.get()
            int st=  ci.get(0).getId();
            System.out.println(st);
            int yhm=hrsShangPinService.intouser(st+1);
            System.out.println("-------"+yhm);
            int yhm1=hrsShangPinService.intouser1(st+1);
            System.out.println("----"+yhm1);
            return "/enroll2";//成功跳注册
        }else{
            return "";
        }

    }
}
